package com.luck.easyfoodbatch.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.adapter.SaleAreaAdapter;
import com.luck.easyfoodbatch.db.FindDB;
import com.luck.easyfoodbatch.entity.City;
import com.luck.easyfoodbatch.entity.CityEntity;
import com.luck.easyfoodbatch.indexablelistview.IndexEntity;
import com.luck.easyfoodbatch.indexablelistview.IndexableStickyListView;
import com.luck.easyfoodbatch.mvp.mine.AvailableSaleAreaModel;
import com.luck.easyfoodbatch.mvp.mine.AvailableSaleAreaPresenter;
import com.luck.easyfoodbatch.mvp.mine.AvailableSaleAreaView;
import com.luck.easyfoodbatch.mvp.model.AvailableM;
import com.luck.easyfoodbatch.util.HttpUtil;
import com.luck.easyfoodbatch.util.T;
import com.luck.easyfoodbatch.util.Tools;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AvailableSaleActivity extends BaseActivity implements AvailableSaleAreaView {

    @BindView(R.id.left_back)
    ImageButton leftBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;
    @BindView(R.id.input_search_query)
    EditText inputSearchQuery;
    @BindView(R.id.searchBarContainer)
    RelativeLayout searchBarContainer;
    @BindView(R.id.indexListView)
    IndexableStickyListView indexListView;
    @BindView(R.id.ll_del)
    LinearLayout llDel;
    private SaleAreaAdapter mAdapter;
    private List<CityEntity> mCities = new ArrayList<>();
    private String tempId = "", gid = "";
    private AvailableSaleAreaModel saleAreaModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location, "推广地区", "确定", MODE_BACK_NAVIGATION);
        ButterKnife.bind(this);
        saleAreaModel = new AvailableSaleAreaPresenter(this, mContext);
        gid = getIntent().getStringExtra("gid");
        showProgressDialog(getString(R.string.app_loading));
        saleAreaModel.requestAvailableSaleArea(gid);
        tempId = getIntent().getStringExtra("tempId");// 选中的地区id

        inputSearchQuery.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (editable.length() > 0) {
                    llDel.setVisibility(View.VISIBLE);
                } else {
                    llDel.setVisibility(View.GONE);
                }
            }
        });
    }

    @OnClick({R.id.ll_del})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_del:
                inputSearchQuery.setText("");
                break;
        }
    }

    @Override
    public void rightClick() {
        super.rightClick();
        StringBuffer sb1 = new StringBuffer();
        StringBuffer sb2 = new StringBuffer();
        for (CityEntity item : mCities) {
            if (item.isChecked()) {
                sb1.append(item.getId()).append("#");
                sb2.append(item.getName()).append(",");
            }
        }
        String ids = Tools.tempIds(sb1.toString());
        String names = Tools.tempNames(sb2.toString());
        if (Tools.isNull(ids) && Tools.isNull(names)) {
            T.showToast(mContext, "请先选择地区~");
        } else {
            Intent intent = new Intent();
            intent.putExtra("id", ids);
            intent.putExtra("name", names);
            setResult(RESULT_OK, intent);
            finish();
        }
    }

    protected void initView(List<City> provinces) {
        mAdapter = new SaleAreaAdapter(this);
        indexListView.setAdapter(mAdapter);

        // 初始化数据
        for (City item : provinces) {
            CityEntity cityEntity = new CityEntity();
            cityEntity.setName(item.getName());
            cityEntity.setId(item.getId());
            if (!Tools.isNull(tempId)) {
                // 如果有选中的数据
                String[] split = tempId.split("#");
                for (int i = 0; i < split.length; i++) {
                    String id = split[i];
                    if (cityEntity.getId().equals(id)) {
                        cityEntity.setChecked(true);
                    }
                }
            } else {
                cityEntity.setChecked(false);
            }
            mCities.add(cityEntity);
        }

        // 绑定数据
        indexListView.bindDatas(mCities);

        // 搜索
        inputSearchQuery.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                indexListView.searchTextChange(charSequence.toString());
            }

            @Override
            public void afterTextChanged(Editable editable) {
            }
        });

        indexListView.setOnItemContentClickListener(new IndexableStickyListView.OnItemContentClickListener() {
            @Override
            public void onItemClick(View v, IndexEntity indexEntity) {
                if (indexEntity.isChecked()) {
                    indexEntity.setChecked(false);
                } else {
                    indexEntity.setChecked(true);
                }
                mAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void availableSaleAreaSuccess(AvailableM model) {
        if (model != null) {
            if (model.getStatus() == HttpUtil.RESULT_OK) {
                List<AvailableM.DataBean> data = model.getData();
                if (data != null && data.size() > 0) {
                    List<City> provinces = new ArrayList<>();
                    for (AvailableM.DataBean item : data) {
                        City city = new City();
                        city.setId(item.getCid() + "");
                        city.setName(item.getName());
                        provinces.add(city);
                    }
                    initView(provinces);
                }
            } else {
                T.showToast(mContext, model.getMsg());
            }
        }
    }

    @Override
    public void httpStatusError() {

    }

    @Override
    public void hideLoading() {
        hideShowProgress();
    }
}
